package com.ruoyi.party.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.party.system.domain.Member;
import com.ruoyi.party.system.domain.Org;
import com.ruoyi.party.system.service.IMemberService;
import com.ruoyi.party.system.service.IOrgService;

/**
 * 党组织机构Controller
 * 
 * @author 黄宝康
 * @date 2021-04-16
 */
@Controller
@RequestMapping("/business/org")
public class OrgController extends BaseController
{
    private String prefix = "business/org";

    @Autowired
    private IOrgService orgService;
    
    @Autowired
    private IMemberService memberService;

    @RequiresPermissions("business:org:view")
    @GetMapping()
    public String org()
    {
        return prefix + "/org";
    }

    /**
     * 查询党组织机构列表
     */
    @RequiresPermissions("business:org:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Org org)
    {
        startPage();
        List<Org> list = orgService.selectOrgList(org);
        return getDataTable(list);
    }

    /**
     * 导出党组织机构列表
     */
    @RequiresPermissions("business:org:export")
    @Log(title = "党组织机构", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Org org)
    {
        List<Org> list = orgService.selectOrgList(org);
        ExcelUtil<Org> util = new ExcelUtil<Org>(Org.class);
        return util.exportExcel(list, "党组织机构数据");
    }

    /**
     * 新增党组织机构
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存党组织机构
     */
    @RequiresPermissions("business:org:add")
    @Log(title = "党组织机构", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated Org org)
    {
        return toAjax(orgService.insertOrg(org));
    }

    /**
     * 修改党组织机构
     */
    @GetMapping("/edit/{orgId}")
    public String edit(@PathVariable("orgId") String orgId, ModelMap mmap)
    {
        Org org = orgService.selectOrgById(orgId);
        mmap.put("org", org);
        return prefix + "/edit";
    }

    /**
     * 修改保存党组织机构
     */
    @RequiresPermissions("business:org:edit")
    @Log(title = "党组织机构", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated Org org)
    {
        return toAjax(orgService.updateOrg(org));
    }

    /**
     * 删除党组织机构
     */
    @RequiresPermissions("business:org:remove")
    @Log(title = "党组织机构", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(orgService.deleteOrgByIds(ids));
    }
    
    // 查看所有的党员
    @GetMapping("/findMembers/{orgId}")
    public String findMembers(@PathVariable("orgId") String orgId,ModelMap mmap)
    {
    	mmap.put("orgId",orgId);
        return prefix + "/allmember";
    }
}
